//
//  ViewController.m
//  test-git
//
//  Created by IOS_wt on 15/12/2.
//  Copyright © 2015年 wangteng. All rights reserved.
//

#import "ViewController.h"
#import "myBestTableViewCell.h"
@interface ViewController ()
{
     UIImagePickerController* _imagePiker;
    
    NSMutableArray* _words;
    NSInteger i;
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.myTableView=[[UITableView alloc]init];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.translatesAutoresizingMaskIntoConstraints=NO;
    self.myTableView.estimatedRowHeight=44.0;
    self.myTableView.rowHeight=UITableViewAutomaticDimension;
    
    self->_words=[[NSMutableArray alloc]init];
    NSArray* a=[NSArray arrayWithObject:@"在我们改变View的layoutMargins这个属性时，会触发- (void)layoutMarginsDidChange这个方法。我们在自己的View里面可以重写这个方法来捕获layoutMargins的变化。在大多数情况下，我们可以在这个方法里触发drawing和layout的Update。preservesSuperviewLayoutMargins这个属性默认是NO。如果把它设为YES，layoutMargins会根据屏幕中相关View的布局而改变。"];
    NSArray* b=[NSArray arrayWithObject:@"系统会调用updateConstraints去更新布局。updateConstraints ：自定义View时，我们应该重写这个方法来设置当前view局部的布局约束。重写这个方法时，一定要调用[super updateConstraints]。"];
    NSArray* c=[NSArray arrayWithObject:@"lpppppppppooooolklklklooooookokoookjikjijiiiijiiii"];
    [self->_words addObjectsFromArray:a];
    [self->_words addObjectsFromArray:b];
    [self->_words addObjectsFromArray:c];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_myTableView]|"options:0 metrics:nil views:@{@"_myTableView":self->_myTableView}]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[_myTableView]|" options:0 metrics:nil views:@{@"_myTableView":self->_myTableView}]];
    
    
    self.myTableView.dataSource=self;
    self.myTableView.delegate=self;
  
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self->_words.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString* cellID=@"cell";
    myBestTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell==nil) {
        cell=[[myBestTableViewCell alloc]init];
    }
    
    [cell awakeFromNib];
    
    cell.mLabel.text=[self->_words objectAtIndex:indexPath.row];
    
    switch (indexPath.row) {
        case 0:
            cell.mImage.tag=indexPath.row;
            break;
        case 1:
            cell.mImage.tag=indexPath.row;
            break;
        case 2:
            cell.mImage.tag=indexPath.row;
            break;
    
    }
    
    //创建手势
    UITapGestureRecognizer* gesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectHeaderImage:)];
    //将手势添加到image控件上
    [cell.mImage addGestureRecognizer:gesture];
//    cell.mImage.layer.cornerRadius=cell.mImage.bounds.size.height/2;
//    cell.mImage.clipsToBounds=YES;
    
    if (cell.mImage.image==nil) {
        cell.mImage.image=[UIImage imageNamed:@"cb3.png"];
    }
    return cell;
    
    
}
//响应选择头像的手势
-(void)selectHeaderImage:(UITapGestureRecognizer*)gesture{
    
     i=gesture.view.tag;
    
    if (self->_imagePiker==nil) {
        self->_imagePiker=[[UIImagePickerController alloc]init];
        self->_imagePiker.delegate=self;
    }
        //是否有摄像机
    BOOL hasCamera=[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];//
    
    if (hasCamera) {
        
        UIAlertController* alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"选择图片来源" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* a1=[UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self presentViewController:self->_imagePiker animated:YES completion:nil];
        }];
        UIAlertAction* a2=[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            self->_imagePiker.sourceType=UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self->_imagePiker animated:YES completion:nil];
        }];
        UIAlertAction* a3=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        
        [alert addAction:a1];
        [alert addAction:a2];
        [alert addAction:a3];
        //模态呈现提示页面
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        [self presentViewController:self->_imagePiker animated:YES completion:nil];
    }
    
}
//UIImagePickerControllerDelegate方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    myBestTableViewCell*cell=(myBestTableViewCell*)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    switch (cell.mImage.tag) {
        case 0:
        {
            cell.mImage.image=image;
            break;
        }
        case 1:
        {
            cell.mImage.image=image;
            break;
        }
        case 2:
        {
            cell.mImage.image=image;
            break;
        }

        
    }
    cell.mImage.image=image;
    
    [self->_imagePiker dismissViewControllerAnimated:YES completion:nil];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
